package com.dream.libxrec.player.audio;

public class G711UDecoder {
    private static final short[] a2s = {
            (short) 60032, (short) 60288, (short) 59520, (short) 59776, (short) 61056, (short) 61312, (short) 60544, (short) 60800,
            (short) 57984, (short) 58240, (short) 57472, (short) 57728, (short) 59008, (short) 59264, (short) 58496, (short) 58752,
            (short) 62784, (short) 62912, (short) 62528, (short) 62656, (short) 63296, (short) 63424, (short) 63040, (short) 63168,
            (short) 61760, (short) 61888, (short) 61504, (short) 61632, (short) 62272, (short) 62400, (short) 62016, (short) 62144,
            (short) 43520, (short) 44544, (short) 41472, (short) 42496, (short) 47616, (short) 48640, (short) 45568, (short) 46592,
            (short) 35328, (short) 36352, (short) 33280, (short) 34304, (short) 39424, (short) 40448, (short) 37376, (short) 38400,
            (short) 54528, (short) 55040, (short) 53504, (short) 54016, (short) 56576, (short) 57088, (short) 55552, (short) 56064,
            (short) 50432, (short) 50944, (short) 49408, (short) 49920, (short) 52480, (short) 52992, (short) 51456, (short) 51968,
            (short) 65192, (short) 65208, (short) 65160, (short) 65176, (short) 65256, (short) 65272, (short) 65224, (short) 65240,
            (short) 65064, (short) 65080, (short) 65032, (short) 65048, (short) 65128, (short) 65144, (short) 65096, (short) 65112,
            (short) 65448, (short) 65464, (short) 65416, (short) 65432, (short) 65512, (short) 65528, (short) 65480, (short) 65496,
            (short) 65320, (short) 65336, (short) 65288, (short) 65304, (short) 65384, (short) 65400, (short) 65352, (short) 65368,
            (short) 64160, (short) 64224, (short) 64032, (short) 64096, (short) 64416, (short) 64480, (short) 64288, (short) 64352,
            (short) 63648, (short) 63712, (short) 63520, (short) 63584, (short) 63904, (short) 63968, (short) 63776, (short) 63840,
            (short) 64848, (short) 64880, (short) 64784, (short) 64816, (short) 64976, (short) 65008, (short) 64912, (short) 64944,
            (short) 64592, (short) 64624, (short) 64528, (short) 64560, (short) 64720, (short) 64752, (short) 64656, (short) 64688,
            (short) 5504, (short) 5248, (short) 6016, (short) 5760, (short) 4480, (short) 4224, (short) 4992, (short) 4736,
            (short) 7552, (short) 7296, (short) 8064, (short) 7808, (short) 6528, (short) 6272, (short) 7040, (short) 6784,
            (short) 2752, (short) 2624, (short) 3008, (short) 2880, (short) 2240, (short) 2112, (short) 2496, (short) 2368,
            (short) 3776, (short) 3648, (short) 4032, (short) 3904, (short) 3264, (short) 3136, (short) 3520, (short) 3392,
            (short) 22016, (short) 20992, (short) 24064, (short) 23040, (short) 17920, (short) 16896, (short) 19968, (short) 18944,
            (short) 30208, (short) 29184, (short) 32256, (short) 31232, (short) 26112, (short) 25088, (short) 28160, (short) 27136,
            (short) 11008, (short) 10496, (short) 12032, (short) 11520, (short) 8960, (short) 8448, (short) 9984, (short) 9472,
            (short) 15104, (short) 14592, (short) 16128, (short) 15616, (short) 13056, (short) 12544, (short) 14080, (short) 13568,
            (short) 344, (short) 328, (short) 376, (short) 360, (short) 280, (short) 264, (short) 312, (short) 296,
            (short) 472, (short) 456, (short) 504, (short) 488, (short) 408, (short) 392, (short) 440, (short) 424,
            (short) 88, (short) 72, (short) 120, (short) 104, (short) 24, (short) 8, (short) 56, (short) 40,
            (short) 216, (short) 200, (short) 248, (short) 232, (short) 152, (short) 136, (short) 184, (short) 168,
            (short) 1376, (short) 1312, (short) 1504, (short) 1440, (short) 1120, (short) 1056, (short) 1248, (short) 1184,
            (short) 1888, (short) 1824, (short) 2016, (short) 1952, (short) 1632, (short) 1568, (short) 1760, (short) 1696,
            (short) 688, (short) 656, (short) 752, (short) 720, (short) 560, (short) 528, (short) 624, (short) 592,
            (short) 944, (short) 912, (short) 1008, (short) 976, (short) 816, (short) 784, (short) 880, (short) 848
    };

    //change G711 ulaw start
    private static final int[] _u2a = { 					/* u- to A-law conversions */
            1,		1,		2,		2,		3,		3,		4,		4,
            5,		5,		6,		6,		7,		7,		8,		8,
            9,		10, 	11, 	12, 	13, 	14, 	15, 	16,
            17, 	18, 	19, 	20, 	21, 	22, 	23, 	24,
            25, 	27, 	29, 	31, 	33, 	34, 	35, 	36,
            37, 	38, 	39, 	40, 	41, 	42, 	43, 	44,
            46, 	48, 	49, 	50, 	51, 	52, 	53, 	54,
            55, 	56, 	57, 	58, 	59, 	60, 	61, 	62,
            64, 	65, 	66, 	67, 	68, 	69, 	70, 	71,
            72, 	73, 	74, 	75, 	76, 	77, 	78, 	79,
            81, 	82, 	83, 	84, 	85, 	86, 	87, 	88,
            89, 	90, 	91, 	92, 	93, 	94, 	95, 	96,
            97, 	98, 	99, 	100,	101,	102,	103,	104,
            105,	106,	107,	108,	109,	110,	111,	112,
            113,	114,	115,	116,	117,	118,	119,	120,
            121,	122,	123,	124,	125,	126,	127,	128};

    /**
     * G711U转G711A
     * @param uval
     * @return
     */
    private static int g711u2G77a(int uval) {
        return ((uval&0x80)!=0)? (0xD5^(_u2a[0xFF^uval]-1)) : (0x55^(_u2a[0x7F^uval]-1));
    }

    /**
     * G711U转PCM
     */
    public static void g711u2PCM(byte[] ulaw,short[] lin,
                                 int offset,int size) {
        for (int i = 0; i < size; i++){
            lin[i] = a2s[g711u2G77a(ulaw[i+offset] & 0xff)];
        }

    }
}
